package crmdna.member;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.googlecode.objectify.ObjectifyFilter;

import crmdna.client.Client;
import crmdna.group.Group;
import crmdna.group.Group.GroupProp;
import crmdna.user.User;
import crmdna.user.UserCore;

public class MemberCoreTest {

	private final LocalServiceTestHelper datastoreHelper = new LocalServiceTestHelper(
			new LocalDatastoreServiceTestConfig().setApplyAllHighRepJobPolicy());

	private final String client = "isha";
	private final String validUser = "valid@login.com";
	private GroupProp sgp;
	private GroupProp kl;
	private GroupProp sydney;

	@Before
	public void setUp() {
		datastoreHelper.setUp();
		ObjectifyFilter.complete();

		Client.create(client);
		sgp = Group.create(client, "Singapore", User.SUPER_USER);
		assertEquals(1, sgp.groupId);

		kl = Group.create(client, "Malaysia/KL", User.SUPER_USER);
		assertEquals(2, kl.groupId);

		sydney = Group.create(client, "Australia/Sydney", User.SUPER_USER);
		assertEquals(3, sydney.groupId);

		User.create(client, validUser, sgp.groupId, User.SUPER_USER);
		assertEquals(1, UserCore.get(client, validUser).toProp().userId);
	}

	@After
	public void tearDown() {
		ObjectifyFilter.complete();
		datastoreHelper.tearDown();
	}

//	@Test
//	public void populateContactDetailsTest() {
//		// only not null values should be copied
//		MemberEntity memberEntity = MemberFactory.create(client);
//		memberEntity.firstName = "Sathyanarayanan";
//		memberEntity.email = "sathya.t@ishafoundation.org";
//		memberEntity.mobilePhone = "+6598361844";
//		memberEntity.homePhone = "+6565227020";
//
//		ContactProp contact = new ContactProp();
//		contact.mobilePhone = "+6593232152";
//
//		memberEntity = MemberCore.populateContactDetails(memberEntity, contact);
//		assertEquals("Sathyanarayanan", memberEntity.firstName);
//		assertEquals(null, memberEntity.lastName);
//		assertEquals("sathya.t@ishafoundation.org", memberEntity.email);
//		assertEquals("+6565227020", memberEntity.homePhone);
//		assertEquals("+6593232152", contact.mobilePhone); // new mobile number
//		assertEquals("Sathyanarayanan".toLowerCase(), memberEntity.name);
//		assertEquals("s", memberEntity.nameFirstChar);
//
//		// quick search tags
//		assertTrue(memberEntity.qsTags.contains("sat"));
//		assertTrue(memberEntity.qsTags.contains("t@i"));
//		assertTrue(memberEntity.qsTags.contains("932"));
//		assertTrue(memberEntity.qsTags.contains("152"));
//
//		// should not contain old number
//		assertTrue(!memberEntity.qsTags.contains("844"));
//	}

//	@Test
//	public void populateContactDetails2Test() {
//		MemberEntity memberEntity = MemberFactory.create(client);
//
//		ContactProp contact = new ContactProp();
//		contact.firstName = "Sathya";
//		contact.asOfyyyymmdd = 20140531; 
//
//		boolean changed = Member.populateContactDetails(memberEntity,
//				contact);
//		assertEquals(true, changed);
//		assertEquals(contact.firstName, memberEntity.firstName);
//		assertEquals(20140531, memberEntity.asOfYYYYMMDD);
//		assertEquals(null, memberEntity.lastName);
//		
//		//change first name with same as of date
//		contact.firstName = "Sathyanarayanan";
//		contact.asOfyyyymmdd = 20140531;
//		changed = Member.populateContactDetails(memberEntity,
//				contact);
//		assertEquals(true, changed);
//		assertEquals(contact.firstName, memberEntity.firstName);
//		assertEquals(20140531, memberEntity.asOfYYYYMMDD);
//		assertEquals(null, memberEntity.lastName);
//		
//		//add same first name for a later date. memberEntity should not change
//		changed = MemberCore.populateContactDetails2(memberEntity,
//				contact, 20140630);
//		assertEquals(false, changed);
//		assertEquals(contact.firstName, memberEntity.firstName);
//		assertEquals(20140531, memberEntity.asOfYYYYMMDD);
//		assertEquals(null, memberEntity.lastName);
//				
//		//add home address from an earlier date
//		//change should not take effect
//		contact.homeAddress.city = "Chennai";
//		changed = MemberCore.populateContactDetails2(memberEntity,
//				contact, 20140430);
//		assertEquals(false, changed);
//		assertEquals(contact.firstName, memberEntity.firstName);
//		assertEquals(20140531, memberEntity.asOfYYYYMMDD);
//		assertEquals(null, memberEntity.lastName);
//		assertEquals(null, memberEntity.homeCity);
//		
//		//add phone number
//		contact.mobilePhone = "+6598361844";
//		changed = MemberCore.populateContactDetails2(memberEntity,
//				contact, 20140531);
//		assertEquals(true, changed);
//		assertEquals(contact.firstName, memberEntity.firstName);
//		assertEquals(20140531, memberEntity.asOfYYYYMMDD);
//		assertEquals(null, memberEntity.lastName);
//		assertEquals("Chennai", memberEntity.homeCity);
//		assertEquals("+6598361844", memberEntity.mobilePhone);
//		
//		//change phone number from a later date
//		contact.mobilePhone = "+6593232152";
//		changed = MemberCore.populateContactDetails2(memberEntity,
//				contact, 20140630);
//		assertEquals(true, changed);
//		assertEquals(contact.firstName, memberEntity.firstName);
//		assertEquals(20140630, memberEntity.asOfYYYYMMDD);
//		assertEquals(null, memberEntity.lastName);
//		assertEquals("Chennai", memberEntity.homeCity);
//		assertEquals("+6593232152", memberEntity.mobilePhone);
//	}

//	@Test
//	public void safeAddToMemcacheTest() {
//		String email = "email@email.com";
//
//		MemberCore.safeAddToMemcache(client, email); // no exception
//
//		// adding again causes exception
//		try {
//			MemberCore.safeAddToMemcache(client, email);
//			assertTrue(false);
//		} catch (APIException ex) {
//			assertEquals(Status.ERROR_RESOURCE_ALREADY_EXISTS, ex.statusCode);
//		}
//
//		// adding again causes exception
//		try {
//			MemberCore.safeAddToMemcache(client, email);
//		} catch (APIException ex) {
//			assertEquals(Status.ERROR_RESOURCE_ALREADY_EXISTS, ex.statusCode);
//		}
//
//		// can add for another client
//		String client2 = "client2";
//		Client.create(client2);
//		MemberCore.safeAddToMemcache(client2, email); // no exception
//
//		// adding again causes exception
//		try {
//			MemberCore.safeAddToMemcache(client2, email);
//		} catch (APIException ex) {
//			assertEquals(Status.ERROR_RESOURCE_ALREADY_EXISTS, ex.statusCode);
//		}
//	}

//	@Test
//	public void safeGetTest() {
//		ContactProp contact = new ContactProp();
//		contact.email = "Sathya.t@IshaFoundation.org";
//		contact.mobilePhone = "+6598361844";
//		contact.homePhone = "+6565227030";
//
//		MemberProp prop = MemberCore
//				.create(client, sgp.groupId, contact, false);
//
//		prop = MemberCore.safeGet(client, prop.memberId).toProp();
//		assertEquals("sathya.t@ishafoundation.org", prop.contact.email);
//		assertEquals("+6598361844", prop.contact.mobilePhone);
//		assertEquals("+6565227030", prop.contact.homePhone);
//	}

	@Test
	public void populateDependantIndexFieldsTest() {
		assertTrue(false);
	}

	@Test
	public void populateDependantFieldsWithoutDBQueryTest() {
		assertTrue(false);
	}
}
